package com.atguigu.gamll_logger.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author chenlongStart
 * @create 2021-06-23 10:56
 * @desc
 */

@RestController
@Slf4j
public class LoggerController {

    @RequestMapping("/applog")
    public String doLog(@RequestParam("param") String logStr){
        //1、把数据写入到日志文件中(给离线需求准备)
        write2File(logStr);

        //2、把数据直接写入到Kafka中
        send2Kafka(logStr);
        return "ok";
    }


    @Autowired
    KafkaTemplate<String,String> kafka;
    private void send2Kafka(String logStr) {
        kafka.send("ods_log",logStr);
    }

    private void write2File(String logStr) {
        log.info(logStr);
    }
}
